<?php

declare (strict_types=1);

namespace App\Model;

use App\Service\FlyShopApplyService;
use App\Service\FlyShopService;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;

/**
 * @property int $id 店铺ID
 * @property string $shop_name 店铺名称
 * @property int $type 店铺类型：1个人 2个体工商户 3企业
 * @property int $join_time 入驻时间
 * @property int $user_id 用户ID
 * @property int $last_up_time 上次上线时间
 * @property string $industry_ids 行业类型列表
 * @property string $dc_ids 集散地列表
 * @property string $business_scope 主营范围
 * @property string $shop_desc 商户介绍
 * @property int $address_id 商家地址ID
 * @property string $address_info 商家详细地址
 * @property string $link_name 商家联系人
 * @property string $phone 商家电话
 * @property int $shop_state 店铺状态：0已提交入驻待审核 1已上架 2已下架 3已过期（非抓取商家）
 * @property int $shop_type 店铺类型 1云店 2云店商城 3抓取商家
 */
class FlyShop extends CloudBase
{
    public $timestamps = false;

    /**
     * @var string 数据库时间类型:时间戳
     */
//    protected $dateFormat = 'U';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_shop';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'type' => 'integer',
        'join_time' => 'integer',
        'user_id' => 'string',
        'down_shelf_user' => 'string',
        'last_up_time' => 'integer',
        'address_id' => 'integer',
        'shop_state' => 'integer',
        'shop_type' => 'integer',
        'service_end_time' => 'integer',
        'open_time' => 'array',
        'meal_set.consume_type' => 'array',
        'create_time' => 'integer'
    ];

    /**
     * 关联集散地
     * @return HasOne
     */
    public function with_join_dc()
    {
        return $this->hasOne(DistributionCenter::class, 'id', 'join_dc_id');
    }

    /**
     * 获取关联申请单
     * @return HasMany
     */
    public function with_join_order()
    {
        return $this->hasMany(FlyShopApply::class, 'shop_id', 'id');
    }

    /**
     * 检查店铺名
     * @param string $shop_name
     * @param bool $except_me
     * @param int $user_id
     * @return bool
     */
    public function checkShopName($shop_name = '', $except_me = false, int $user_id = 0): bool
    {
        return self::query(true)->where(function ($where) use ($shop_name, $except_me, $user_id) {
            $where->where('shop_name', '=', $shop_name);
            if ($except_me) {
                $where->where('user_id', '<>', $user_id);
            }
            $where->whereNull('delete_time');
        })->exists();
    }

    /**
     * 获取行业
     * @return HasMany
     */
    public function with_industry()
    {
        return $this->hasMany(FlyShopSet::class, 'shop_id', 'id')
            ->where('fly_shop_sets.type', '=', 1)
            ->with('industry_info');
    }

    /**
     * 获取集散地
     * @return object
     */
    public function center_info()
    {
        return $this->hasOne(DistributionCenter::class, 'id', 'join_dc_id')
            ->with(['province', 'city', 'area'])
            ->select(['id', 'name', 'province_id', 'city_id', 'area_id']);
    }

    /**
     * 获取完善信息
     * @return object
     */
    public function apply_info()
    {
        return $this->belongsTo(FlyShopApply::class, 'id', 'shop_id')
            ->leftJoin('fly_shop_applys_info as info', 'info.apply_id', '=', 'fly_shop_applys.id')
            ->where([
//                'fly_shop_applys.status' => 2,
                'fly_shop_applys.shop_style' => 1,
            ])->select(['fly_shop_applys.*', 'info.figure_urls']);
    }

    /**
     * 获取完善信息
     * @return object
     */
    public function apply_food_info()
    {
        return $this->belongsTo(FlyShopApply::class, 'id', 'shop_id')
            ->leftJoin('fly_shop_applys_info as info', 'info.apply_id', '=', 'fly_shop_applys.id')
            ->where([
//                'status' => 2,
                'fly_shop_applys.shop_style' => 2,
//                'apply_order_type'=>2,
            ])->select(['fly_shop_applys.*', 'info.figure_urls']);
    }


//    形象照片
    public function certificate_att()
    {
        return $this->hasMany(FlyShopCertificateAtt::class, 'shop_id', 'id')
            ->with('file_info')
            ->where([
                'type' => 1,
            ]);
    }

    /**
     * 认证信息
     * @return HasOne
     */
    public function with_certificate()
    {
        return $this->hasOne(FlyShopCertificate::class, 'shop_id', 'id');
    }

    /**
     * 订餐设置详情
     * @return HasOne
     */
    public function reserved_set()
    {
        return $this->belongsTo(ReservedSeatsSet::class,'id', 'shop_id');
    }

    /**
     * 订餐设置详情
     * @return HasOne
     */
    public function meal_set()
    {
        return $this->belongsTo(FlyGourmetMealConf::class, 'id', 'shop_id');
    }

//    已审核美食列表
    public function food_list()
    {
        return $this->hasMany(Product::class,'shop_id','id')
            ->with([
                'cate_info',
                'main_pic',
                'file_info',
                'sku_list'
            ])
//            ->with(['reason_info', 'refuse_info', 'shop_info'])
            ->where('fly_product.is_illegal','0')
            ->where('fly_product.is_show','1')
            ->where('fly_product.check_status','1')
            ->where('fly_product.type','2')
            ;
    }
//
    //    已审核产品列表
    public function product_list()
    {
        return $this->hasMany(Product::class,'shop_id','id')
            ->with([
                'cate_info',
                'main_pic',
                'file_info',
                'sku_list'
            ])
//            ->with(['reason_info', 'refuse_info', 'shop_info'])
            ->where('fly_product.is_illegal','0')
            ->where('fly_product.is_show','1')
            ->where('fly_product.check_status','1')
        ->where('fly_product.type','1');
    }



}